package org.example.function;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class Java8Function4 {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("node", "c++", "java", "javascript");
        List<String> result = mapToList(list, Java8Function4::hash256);
        System.err.println(JSONObject.toJSONString(result));
        List<String> result2 = mapToList(list, s -> DigestUtils.sha256Hex(s));
        System.err.println(JSONObject.toJSONString(result2));
    }

    static <T,R> List<R> mapToList(List<T> list, Function<T,R> func){
        List<R> results = new ArrayList<>();
        for (T t : list) {
            results.add(func.apply(t));
        }
        return results;
    }

    static String hash256(String str){
        return DigestUtils.sha256Hex(str);
    }
}

